#ifndef __KHELPER_H__
#define __KHELPER_H__

#ifdef KHELPER_EXPORTS
#define KHELPER_API __declspec(dllexport)
#else
#define KHELPER_API __declspec(dllimport)
#endif



#ifdef __cplusplus
extern "C"{
#endif



/***********************************************************************
kProcess starts*/

KHELPER_API BOOL  WINAPI kRemoteCall(
									 HANDLE hProcess,
									 FARPROC proc,
									 DWORD val,
									 DWORD *ret
									 );
KHELPER_API BOOL  WINAPI kRemoteCallWithData(
	HANDLE hProcess,
	FARPROC proc,
	void* data,
	SIZE_T size,
	DWORD *ret
	);

KHELPER_API HMODULE  WINAPI kRemoteLoadLibraryW(
								HANDLE hProcess,
								LPCWSTR lpLibraryName
								);
#define kRemoteLoadLibrary kRemoteLoadLibraryW;

KHELPER_API HMODULE WINAPI kRemoteLoadLibraryWithWindowW(
	LPCWSTR lpClassName,
	LPCWSTR lpWindowName,
	LPCWSTR lpLibraryName
	);
#define kRemoteLoadLibraryWithWindow kRemoteLoadLibraryWithWindowW

KHELPER_API HMODULE WINAPI kRemoteLoadLibraryWithFileW(
	LPCWSTR lpFileName/*case sensitive and in device form, see GetProcessImageFileName*/,
	LPCWSTR lpLibraryName
	);
#define kRemoteLoadLibraryWithFile kRemoteLoadLibraryWithFileW

KHELPER_API BOOL  WINAPI kRemoteFreeLibrary(
								HANDLE hProcess,
								HMODULE hModule
								);


KHELPER_API
BOOL
WINAPI
kCreateProcessW(
    IN LPCWSTR lpApplicationName,
    IN LPWSTR lpCommandLine,
    IN LPSECURITY_ATTRIBUTES lpProcessAttributes,
    IN LPSECURITY_ATTRIBUTES lpThreadAttributes,
    IN BOOL bInheritHandles,
    IN DWORD dwCreationFlags,
    IN LPVOID lpEnvironment,
    IN LPCWSTR lpCurrentDirectory,
    IN LPSTARTUPINFOW lpStartupInfo,	
    OUT LPPROCESS_INFORMATION lpProcessInformation,
	IN LPCWSTR lpLibraryName,
	OUT HMODULE *phMod
    );
#define kCreateProcess  kCreateProcessW

KHELPER_API BOOL WINAPI kDebugMode(BOOL bToggle);



//suspend all threads in specified process except self(if exists)
KHELPER_API BOOL WINAPI   kSuspendAllThreads(DWORD processId);

//resumn all threads in specified process excepts self
KHELPER_API BOOL WINAPI   kResumeAllThreads(DWORD processId);

KHELPER_API BOOL WINAPI kSendMessageWithData(HWND hWnd,DWORD msg,DWORD wParam,void *pMem,size_t size);



/***********************************************************************
kModule starts
*/
KHELPER_API BOOL WINAPI kSetProcAddressOneModA(
	HANDLE hProcess,
	HMODULE hMod,
	LPCSTR lpLibraryName,
	FARPROC origProc,
	FARPROC proc);
#define kSetProcAddressOneMod kSetProcAddressOneModA


//load the library and fix it up to call our function
KHELPER_API HMODULE WINAPI kLoadLibraryW(LPCWSTR lpLibraryName);
KHELPER_API HMODULE WINAPI kLoadLibraryA(LPCSTR lpLibraryName);
#define kLoadLibrary kLoadLibraryA;

//return hooked function if any, or return real function
KHELPER_API FARPROC WINAPI kGetProcAddress(HMODULE hModule,LPCSTR lpProcName);

//return real procedure address
KHELPER_API FARPROC WINAPI kGetRealProcAddress(HMODULE hModule,LPCSTR lpProcName);

KHELPER_API BOOL WINAPI kSetProcAddressA(
	LPCSTR lpLibraryName,
	LPCSTR lpProcName,
	FARPROC proc,
	HMODULE pass
	);
#define kSetProcAddress kSetProcAddressA




KHELPER_API HMODULE WINAPI   kGetModuleFromAddress(PVOID pv);
#define kGetCurrentModule() kGetModuleFromAddress(__FUNCTION__)




/***********************************************************************
kCodeFix starts
*/

//fixup orig call to new call,
//assum code context
//           NOP NOP NOP NOP NOP
//eip-->mov edi,edi
//
KHELPER_API BOOL WINAPI kFixupCallEx(HANDLE hProcess,FARPROC srcproc,FARPROC dstproc,FARPROC *realProc);
KHELPER_API BOOL WINAPI kFixupCall(FARPROC srcproc,FARPROC dstproc,FARPROC *realProc);


//wirte back folowing code
//assum code context
//           NOP NOP NOP NOP NOP
//eip-->mov edi,edi
//
KHELPER_API BOOL WINAPI kCleanFixupCallEx(HANDLE hProcess,FARPROC srcproc);
KHELPER_API BOOL WINAPI kCleanFixupCall(FARPROC srcproc);

/**
@fixup one call at targetpos to our destcall
@param targetPos whick contains the call opcode
@origCall set to original call if not null
**/
KHELPER_API BOOL WINAPI kFixupOneE8CallEx(HANDLE hProcess,void *targetPos,FARPROC dstCall,FARPROC *origCall);
KHELPER_API BOOL WINAPI kFixupOneE8Call(void *targetPos,FARPROC dstCall,FARPROC *origCall);

//orig must not be the same as buffer
KHELPER_API BOOL WINAPI kWriteMemEx(HANDLE hProcess,void *targetPos,void *buffer,DWORD size,void *orig);
KHELPER_API BOOL WINAPI kWriteMem(void *targetPos,void *buffer,DWORD size,void *orig);

KHELPER_API BOOL WINAPI kWriteMem4BEx(HANDLE hProcess,void *targetPos,DWORD val,DWORD *origVal);
KHELPER_API BOOL WINAPI kWriteMem4B(void *targetPos,DWORD val,DWORD *origVal);

#ifdef __cplusplus
}
#endif

#endif